package com.educate.ucenter.shiro.filter;

import lombok.extern.log4j.Log4j2;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Component
@Log4j2
@Order(1)
public class ParamLegalFilter implements Filter {

    public static String uid = "";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.debug("------------ParamLegalFilter Init--------------");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest res = (HttpServletRequest) servletRequest;
        HttpServletResponse rep = (HttpServletResponse) servletResponse;

//        if ("POST".equalsIgnoreCase(res.getMethod())) {
//            res = new WrapperBodyServletRequest(res);
//            try {
//                JSONObject jsonObject = new JSONObject(content);
//                uid = jsonObject.getString("uid");
//                jsonObject.put("uid", "userId");
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//        }
        uid = res.getHeader("uid");
        System.out.println("uid = " + uid);

        filterChain.doFilter(res, rep);
    }

    @Override
    public void destroy() {

    }

    public static class WrapperBodyServletRequest extends HttpServletRequestWrapper {

        private final byte[] body;

        public WrapperBodyServletRequest(HttpServletRequest request) throws IOException {
            super(request);
            ServletInputStream stream = request.getInputStream();
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            byte[] buff = new byte[100];
            int rc = 0;
            while ((rc = stream.read(buff, 0, 100)) > 0) {
                swapStream.write(buff, 0, rc);
            }
            this.body = swapStream.toByteArray();
        }

        @Override
        public BufferedReader getReader() throws IOException {
            return new BufferedReader(new InputStreamReader(getInputStream()));
        }

        @Override
        public ServletInputStream getInputStream() throws IOException {
            final ByteArrayInputStream bais = new ByteArrayInputStream(body);
            return new ServletInputStream() {
                @Override
                public boolean isFinished() {
                    return false;
                }

                @Override
                public boolean isReady() {
                    return false;
                }

                @Override
                public void setReadListener(ReadListener readListener) {

                }

                @Override
                public int read() throws IOException {
                    return bais.read();
                }
            };
        }
    }

}
